<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status !== '2'"
                v-hasPermi="['quality:disqualificationDispose:handle']"
                type="text"
                @click.stop="checkout(scope.row)"
              >处置</el-button>
              <span v-else class="el-button--text button_grey">处置</span>
              <el-button
                v-if="scope.row.status !== '2' && scope.row.automaticCreate !== '1'"
                v-hasPermi="['quality:disqualificationDispose:edit']"
                type="text"
                @click.stop="edit(scope.row)"
              >编辑</el-button>
              <span v-else class="el-button--text button_grey">编辑</span>
              <el-button
                v-if="scope.row.status !== '2' "
                v-hasPermi="['quality:disqualificationDispose:delete']"
                type="text"
                @click.stop="deleteOne(scope.row)"
              >删除</el-button>
              <span v-else class="el-button--text button_grey">删除</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { ticketList, remove, getInfo } from '@/api/qualityManage/rejectsDispose'
import { getDictSelects } from '@/api/common/common'
import AddOrUpdate from './addOrUpdate'
import ShowDetail from '@/components/JLINK2/common/ShowDetailTitle'
import moment from 'moment' // 查看页面
import { userList } from '@/api/system/dept'
import SearchBarNew from '@/components/JLink/common/SearchBarNew.vue'
// import chooseFinishOrder from './chooseFinishOrder.vue'
export default {
  name: 'Index',
  components: { SearchBarNew },
  data() {
    return {
      loading: true,
      pageId: '不良品处置单', // 页面唯一标识
      pageExportId: '不良品处置单导出', // 页面导出标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '不良品单号', minWidth: '160' },
        { prop: 'disposeName', label: '处理人员', canOrder: false },
        { prop: 'disposeDate', label: '处理日期' },
        { prop: 'sourceLabel', label: '不良品来源', canOrder: false },
        { prop: 'relatedCheckCode', label: '关联表单', minWidth: '160' },
        { prop: 'materialCode', label: '物料编码', minWidth: '160' },
        { prop: 'materialName', label: '物料名称', minWidth: '160', canOrder: false },
        { prop: 'materialModel', label: '规格型号', minWidth: '100', canOrder: false },
        { prop: 'materialUnitName', label: '物料单位', minWidth: '100', canOrder: false },
        { prop: 'disqualificationNumber', label: '不良数量', canOrder: false, minWidth: this.SETTINGS.settings_decimalDigits_length },
        { prop: 'remark', label: '备注', canOrder: false }
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '状态', canOrder: false, width: '100' },
        { prop: 'code', label: '不良品单号' },
        { prop: 'disposeName', label: '处理人员', canOrder: false },
        { prop: 'disposeDate', label: '处理日期' },
        { prop: 'sourceLabel', label: '不良品来源', canOrder: false },
        { prop: 'relatedCheckCode', label: '关联表单' },
        { prop: 'materialCode', label: '物料编码', minWidth: '200' },
        { prop: 'materialName', label: '物料名称', minWidth: '200', canOrder: false },
        { prop: 'materialModel', label: '规格型号', minWidth: '100', canOrder: false },
        { prop: 'materialUnitName', label: '物料单位', minWidth: '100', canOrder: false },
        { prop: 'disqualificationNumber', label: '不良数量', canOrder: false, minWidth: this.SETTINGS.settings_decimalDigits_length },
        { prop: 'remark', label: '备注', canOrder: false }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'allSearchValue', label: '模糊搜索', type: 'input' },
        // { prop: 'code', label: '不良品单号', type: 'input' },
        // { prop: 'sourceLabel', label: '不良品来源', type: 'input' },
        // { prop: 'materialCode', label: '物料编码', type: 'input' },
        { prop: 'disposeBy', label: '处理人员', type: 'select', optionsName: 'userList' },
        // { prop: 'relatedCheckCode', label: '关联表单', type: 'input' },
        // { prop: 'materialName', label: '物料名称', type: 'input' },
        // { prop: 'materialModel', label: '规格型号', type: 'input' },
        // { prop: 'materialUnitName', label: '物料单位', type: 'input' },
        // { prop: 'disqualificationNumber', label: '不良数量', type: 'input' },
        { prop: 'disposeDate', label: '处理日期', type: 'time', isTime: true },
        { prop: 'statusList', label: '状态', type: 'select-multiple', optionsName: 'statusOptions' }
      ],
      userList: [],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'quality:disqualificationDispose:add' },
        { label: '批量导出3', perm: 'quality:disqualificationDispose:export' },
        { label: '批量删除', perm: 'quality:disqualificationDispose:delete' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [ // 选择组件数据源
        // { value: undefined, label: '全部' },
        { value: '0', label: '待处理' },
        { value: '1', label: '处理中' },
        { value: '2', label: '已处理' }
      ],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '不良品单号', key: 'code' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '处理人员', key: 'disposeName' },
          { type: 'form', name: '处理日期', key: 'disposeDate' },
          { type: 'form', name: '不良品来源', key: 'sourceLabel' },
          { type: 'form', name: '关联表单', key: 'relatedCheckCode' }, // length:'double' 表示占两列
          { type: 'form', name: '物料编码', key: 'materialCode' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '物料名称', key: 'materialName' },
          { type: 'form', name: '规格型号', key: 'materialModel' },
          { type: 'form', name: '物料单位', key: 'materialUnitName' },
          { type: 'form', name: '不良数量', key: 'disqualificationNumber' },
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '不良处理', key: 'data', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '处置方式', key: 'disposeWayLabel' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '处置数量', key: 'disposeNumber' }
            ]
          }
        ]
      }

    }
  },
  created() {
    this.getDictOptions()
    this.getUser()
  },
  mounted() {
  },
  methods: {
    getUser() {
      userList().then(res => {
        this.userList = res.data.map(item => {
          return { value: item.userId, label: item.realName }
        })
      })
    },
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.showDetail(row)
          break
      }
    },
    getDictOptions() {
      getDictSelects('shiftCode').then(res => {
        this.statusOptions2 = res.data
      })
    },
    getExportData(data) {
      return ticketList(data)
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    refresh2() {
      this.tableData = []
      setTimeout(() => {
        this.getList()
      }, 50)
    },
    getList() {
      ticketList(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    showDetail(row) {
      getInfo(row[this.mainId]).then(res => {
        if (!res.data.disposeName) {
          res.data.disposeName = this.$store.state.user.name
          res.data.disposeBy = this.$store.state.user.userId
        }
        if (res.data.disposeDate == null) {
          res.data.disposeDate = moment(new Date()).format('YYYY-MM-DD')
        }
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetail, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看不良品处置单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {}// props
        },
        area: ['1320px', '700px'],
        title: '新增不良品处置单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    checkout(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId],
            show: true
          }// props
        },
        area: ['1320px', '700px'],
        title: '执行不良品处置单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['1320px', '700px'],
        title: '编辑不良品处置单', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.selectionsAll = []
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
